package com.sqx.modules.fiction.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.course.dao.CourseClassificationDao;
import com.sqx.modules.course.entity.CourseClassification;
import com.sqx.modules.fiction.dao.FictionChapterDao;
import com.sqx.modules.fiction.dao.FictionDao;
import com.sqx.modules.fiction.entity.NFiction;
import com.sqx.modules.fiction.entity.NFictionChapter;
import com.sqx.modules.fiction.entity.dto.GetFictionListDto;
import com.sqx.modules.fiction.entity.dto.SelFictionListDto;
import com.sqx.modules.fiction.entity.vo.GetFictionDetailVo;
import com.sqx.modules.fiction.entity.vo.SelFictionListVo;
import com.sqx.modules.fiction.service.FictionService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FictionServiceImpl extends ServiceImpl<FictionDao, NFiction> implements FictionService {
    @Autowired
    private FictionDao fictionDao;
    @Autowired
    private CourseClassificationDao courseClassificationDao;
    @Autowired
    private FictionChapterDao fictionChapterDao;
    @Override
    public Result getFictionList(GetFictionListDto getFictionListDto) {
        Map<Long, String> map = getFictionSortMap();
        Page<NFiction> page = new Page<>(ObjectUtils.isEmpty(getFictionListDto.getPage()) ? 1 : getFictionListDto.getPage(), ObjectUtils.isEmpty(getFictionListDto.getLimit()) ? 10 : getFictionListDto.getLimit());
        LambdaQueryWrapper<NFiction> fictionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fictionLambdaQueryWrapper.eq(NFiction::getFictionDelStatus,1);
        fictionLambdaQueryWrapper.like(ObjectUtils.isNotEmpty(getFictionListDto.getFictionTitle()),NFiction::getFictionTitle,getFictionListDto.getFictionTitle());
        fictionLambdaQueryWrapper.ge(ObjectUtils.isNotEmpty(getFictionListDto.getStartTime()), NFiction::getUpdateTime, getFictionListDto.getStartTime());
        fictionLambdaQueryWrapper.ge(ObjectUtils.isNotEmpty(getFictionListDto.getEndTime()), NFiction::getUpdateTime, getFictionListDto.getEndTime());
        fictionLambdaQueryWrapper.ge(ObjectUtils.isNotEmpty(getFictionListDto.getPublishTimeStart()), NFiction::getPublishTime, getFictionListDto.getPublishTimeStart());
        fictionLambdaQueryWrapper.le(ObjectUtils.isNotEmpty(getFictionListDto.getPublishTimeEnd()), NFiction::getPublishTime, getFictionListDto.getPublishTimeEnd());
        IPage<NFiction> fictionIPage = fictionDao.selectPage(page, fictionLambdaQueryWrapper);
        List<NFiction> fictions = fictionIPage.getRecords().stream().map(o -> {
            o.setCourseClassificationName(map.get(o.getCourseClassificationId()));
            return o;
        }).collect(Collectors.toList());
        fictionIPage.setRecords(fictions);
        return Result.success().put("data",fictionIPage);
    }

    @Override
    public Result getFictionById(Long id) {
        NFiction fiction = fictionDao.selectById(id);
        return Result.success().put("data",fiction);
    }

    @Override
    public Result updateFictionById(NFiction fiction) {
        fictionDao.updateById(fiction);
        return Result.success();
    }

    @Override
    public Result deleteFictionById(Long[] id) {
        fictionDao.deleteById(id);
        return Result.success();
    }

    @Override
    public Result addFiction(NFiction fiction) {
        fiction.setCreateTime(new Date());
        fictionDao.insert(fiction);
        return Result.success();
    }

    @Override
    public Result selFictionList(SelFictionListDto selFictionListDto) {
        Map<Long, String> map = getFictionSortMap();
        Page<NFiction> page = new Page<>(ObjectUtils.isEmpty(selFictionListDto.getPage())?1:selFictionListDto.getPage(),ObjectUtils.isEmpty(selFictionListDto.getLimit())?10:selFictionListDto.getLimit());
        LambdaQueryWrapper<NFiction> fictionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fictionLambdaQueryWrapper.eq(NFiction::getFictionDelStatus,1);
        fictionLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(selFictionListDto.getFictionAudience()), NFiction::getFictionAudience,selFictionListDto.getFictionAudience());
        fictionLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(selFictionListDto.getFictionSortId()), NFiction::getCourseClassificationId,selFictionListDto.getFictionSortId());
        fictionLambdaQueryWrapper.last("ORDER BY RAND()");
        IPage<NFiction> fictionIPage = fictionDao.selectPage(page, fictionLambdaQueryWrapper);
        List<SelFictionListVo> selFictionListVos = fictionIPage.getRecords().stream().map(o -> {
            SelFictionListVo selFictionListVo = new SelFictionListVo();
            BeanUtil.copyProperties(o, selFictionListVo);
            selFictionListVo.setFictionSortName(map.get(o.getCourseClassificationId()));
            return selFictionListVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(selFictionListVos, Math.toIntExact(fictionIPage.getTotal()),
                Math.toIntExact(fictionIPage.getSize()), Math.toIntExact(fictionIPage.getCurrent()));
        return Result.success().put("data",pageUtils);
    }

    @Override
    public Result getFictionDetail(Long fictionId) {
        //计算阅读人数
        Map<Long, String> map = getFictionSortMap();
        NFiction fiction = fictionDao.selectById(fictionId);
        GetFictionDetailVo getFictionDetailVo = new GetFictionDetailVo();
        BeanUtil.copyProperties(fiction,getFictionDetailVo);
        getFictionDetailVo.setFictionSortName(map.get(fiction.getCourseClassificationId()));
        Page<NFictionChapter> page = new Page<>(1, 3);
        LambdaQueryWrapper<NFictionChapter> fictionChapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fictionChapterLambdaQueryWrapper.eq(NFictionChapter::getFictionId,fictionId);
        fictionChapterLambdaQueryWrapper.eq(NFictionChapter::getFictionDelStatus,1);
        fictionChapterLambdaQueryWrapper.orderByAsc(NFictionChapter::getCreateTime);
        fictionChapterDao.selectPage(page,fictionChapterLambdaQueryWrapper);
        getFictionDetailVo.setFictionChapters(page.getRecords());
        getFictionDetailVo.setFictionChapterCount(Math.toIntExact(page.getTotal()));
        return Result.success().put("data",getFictionDetailVo);
    }

    public Map<Long, String> getFictionSortMap() {
        List<CourseClassification> fictionSorts = courseClassificationDao.selectList(null);
        HashMap<Long, String> map = new HashMap<>();
        for (CourseClassification fictionSort : fictionSorts) {
            map.put(fictionSort.getClassificationId(),fictionSort.getClassificationName());
        }
        return map;
    }
}
